<?php


class Nutritionist extends CI_Model{
	
	function validate() {
        $this->db->where('email', $this->input->post('txt_email')); //@TODO sqlinjection
        $this->db->where('password', $this->input->post('txt_password')); //@TODO md5()

        $query = $this->db->get('nutritionist'); 

        if ($query->num_rows == 1) { 
            return $query->row()->id; 
        }else{
			return 0;
		}
    }
	
	function login($id_nutritionist) {
		$newdata = array(
			'id_n'	=> $id_nutritionist,
			'email_n' => $this->input->post('txt_email'),
			'logged_n'		=> TRUE
		);

		$this->session->set_userdata($newdata);
	}
	
	function logout() {
		
		$array_items = array('id_n' => '', 'email_n' => '','logged_n' => '');
		$this->session->unset_userdata($array_items);
		redirect('/', 'refresh');
	}
	
	function is_logged() {
        $logged = $this->session->userdata('logged_n');
		return ($logged);
    }
	
	function logged($url='', $redirect_l=false, $redirect_nl=true) {
        $logged = $this->session->userdata('logged_n');
		if($logged == true){
			if($redirect_l) redirect('/'.$url, 'refresh');
		}
		else {
			if ($redirect_nl) redirect('/login', 'refresh');
		}
    }
}


?>
